package com.cloud.poetry.core.interceptor;

import com.cloud.poetry.core.constant.PoetryConstant;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author mengxj
 */
@Component
public class GatewayInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String token = request.getHeader(PoetryConstant.REQUEST_TYPE_KEY);
        boolean gateway = Objects.equals(PoetryConstant.REQUEST_TYPE_VALUE_GATEWAY, token);
        boolean feign = Objects.equals(PoetryConstant.REQUEST_TYPE_VALUE_FEIGN, token);
        if(!feign){
            request.setAttribute(PoetryConstant.HANDLE_RESPONSE_KEY, PoetryConstant.HANDLE_RESPONSE_VALUE);
        }
        return feign || gateway;
    }
}
